package edu.cuit.avatar.io;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/11 14:45
 */
public class FileTest {
    public static void main(String[] args) {
        File file = new File("D:/home/Welcome.java");
        File file2 = new File("D:/test","clock.html");
        File file3 = new File("D:/test"); //目录抽象表示
        File file4 = new File(file3, "Welcome.java");

        System.out.println(file.exists()); //false
        System.out.println(file2.exists()); //true

        if(file4.exists()){
            System.out.println("文件名:" + file4.getName());
            System.out.println("是否可读:" + file4.canRead());
            System.out.println("是否可写:" + file4.canWrite());
            System.out.println("是否可执行:" + file4.canExecute());
            System.out.println("是否隐藏:" + file4.isHidden());
            System.out.println("绝对路径:" + file4.getAbsolutePath());
            System.out.println(file4.getPath());
            System.out.println("parent path:" + file4.getParent());
            System.out.println("是否为文件:" + file4.isFile());
            System.out.println("是否为目录:" + file4.isDirectory());//false
            System.out.println("文件大小:" + file4.length() + " bytes");

            long lastModified = file4.lastModified();
            var date = new Date(lastModified);
            var sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("最后修改时间:" + sdf.format(date));
        }

        if(file3.isDirectory()){
            File[] files = file3.listFiles();
            for(int i = 0; files != null && i < files.length; i++) {
                System.out.print(files[i].getAbsolutePath());
                if(files[i].isFile())
                    System.out.println(" " + files[i].length() + " bytes");
            }
        }

        System.out.println("===============================================");
        var f = new File("D:/home/2025/07/11", "Welcome.java");
        if(!f.getParentFile().exists())
            f.getParentFile().mkdirs();// 创建层极目录 mkdir -p
    }
}
